import { getToken, setToken } from '@/utils/auth.js'
import { login, sysProfile, sysUser } from '@/api/user'

const state = {
    token: getToken() || '',
    userInfo: ''
}
const mutations = {
    setToken(state, value) {
        state.token = value
        setToken(value)
    },
    setUserInfo(state, value) {
        state.userInfo = value
    },
    logout(state) {
        state.token = ''
        state.userInfo = ''
        setToken('')
    }
}
const actions = {
    // 登录调用
    async toLogin(store, value) {
        const res = await login(value)
        store.commit('setToken', res.data)
    },

    // 获取个人用户信息
    async getUserInfo(store) {
        const res = await sysProfile()
        const res2 = await sysUser(res.data.userId)
        store.commit('setUserInfo', {...res.data, ...res2.data })
        console.log('个人用户信息', res)
    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}